#!/bin/bash

set -e
#Check param
if [ $# -lt 1 ]
then
    echo "Usage: $0 {Platform} "
    echo "Platform:Linux/LinuxEmbedded/Windows32/Windows64/MacOS/iOS/Android"
    exit 1
fi

# version & tarfile & path 
LIBEVENTVERSION=2.1.7-rc
DEPPATH=$(cd `dirname $0`; pwd)
TARBASENAME=libevent-release-2.1.7-rc
TAR=$TARBASENAME.tar.gz
LIBEVENTDIR=$DEPPATH/$TARBASENAME

#set args
PLATFORM=$1
CC=cc
FLAGS="--disable-shared --disable-libevent-regress --disable-samples"
LIBEVENTOUT=`pwd`/libevent-$PLATFORM
PREFIX=--prefix=$LIBEVENTOUT
CONFIG=


spinner()
{
  local pid=$!
  local delay=0.75
  local spinstr='|/-\'
  while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
    local temp=${spinstr#?}
    printf " [%c]  " "$spinstr"
    local spinstr=$temp${spinstr%"$temp"}
    sleep $delay
    printf "\b\b\b\b\b\b"
  done
  printf "    \b\b\b\b"
}

build_Android()
{
    rm -fr $LIBEVENTDIR
    mkdir -p $LIBEVENTDIR
    cd $LIBEVENTDIR

    NDKVERSION=r12                                  
    NDKDIR=/Users/max/Library/Android/android-ndk-$NDKVERSION

    #Build armeabi
    echo "Build libevent for armeabi..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16                                   #修改项
    ARCH=arch-arm                                   #修改项
    SUBPLATFORM=armeabi                             #修改项
    TOOLCHAIN=arm-linux-androideabi-4.9             #修改项
    TOOLNAME=arm-linux-androideabi                  #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-armeabi      #修改项
    CONFIGPLATFORM=android                          #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    export NDK=$NDKDIR
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR >$LOGFILE 2>&1
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-armeabi/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build armv7a
    echo "Build libevent for armv7a..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1
    
    APIVERSION=16                                   #修改项
    ARCH=arch-arm                                   #修改项
    SUBPLATFORM=armeabi-v7a                         #修改项
    TOOLCHAIN=arm-linux-androideabi-4.9             #修改项
    TOOLNAME=arm-linux-androideabi                  #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-armv7a       #修改项
    CONFIGPLATFORM=android-armv7                    #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM        
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    export NDK=$NDKDIR
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR >$LOGFILE 2>&1
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-armv7a/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip

    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build arm64-v8a
    echo "Build libevent for arm64-v8a..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-arm64                                 #修改项
    SUBPLATFORM=arm64-v8a                           #修改项
    TOOLCHAIN=aarch64-linux-android-4.9             #修改项
    TOOLNAME=aarch64-linux-android                  #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-arm64-v8a    #修改项
    CONFIGPLATFORM=linux-generic64                  #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM 
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR

    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-arm64-v8a/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build x86_32
    echo "Build libevent for x86_32..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16
    ARCH=arch-x86                                   #修改项
    SUBPLATFORM=x86                                 #修改项
    TOOLCHAIN=x86-4.9                               #修改项
    TOOLNAME=i686-linux-android                     #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-x86          #修改项
    CONFIGPLATFORM=android-x86                      #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
               
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-x86/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build x86_64
    echo "Build libevent for x86_64..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-x86_64                                #修改项
    SUBPLATFORM=x86_64                              #修改项
    TOOLCHAIN=x86_64-4.9                            #修改项
    TOOLNAME=x86_64-linux-android                   #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-x86_64       #修改项
    CONFIGPLATFORM=linux-x86_64                     #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
               
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR  > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-x86_64/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build mips
    echo "Build libevent for mips_32..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=16                                   #修改项
    ARCH=arch-mips                                  #修改项
    SUBPLATFORM=mips                                #修改项
    TOOLCHAIN=mipsel-linux-android-4.9              #修改项
    TOOLNAME=mipsel-linux-android                   #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-mips         #修改项
    CONFIGPLATFORM="android-mips"                   #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM 
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR
              
    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-mips/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 

    #Build mips64
    echo "Build libevent for mips_64..."
    echo "  tar -xzvf $TAR"
    rm -fr *
    tar -xzvf ../$TAR -C ../ >/dev/null 2>&1

    APIVERSION=21                                   #修改项
    ARCH=arch-mips64                                #修改项
    SUBPLATFORM=mips64                              #修改项
    TOOLCHAIN=mips64el-linux-android-4.9            #修改项
    TOOLNAME=mips64el-linux-android                 #修改项
    HOST=$TOOLNAME
    INSTALLDIR=`pwd`/android-toolchain-mips64       #修改项
    CONFIGPLATFORM="linux-generic64"                #修改项
    PREFIX=--prefix=$LIBEVENTOUT/$SUBPLATFORM
    LOGDIR=$LIBEVENTOUT/$SUBPLATFORM
    LOGFILE=$LOGDIR/libevent-build-$SUBPLATFORM.log
    mkdir -p $LOGDIR     

    export NDK=$NDKDIR
    export ANDROID_DEV=$NDKDIR/platforms/android-$APIVERSION/$ARCH/usr
    $NDK/build/tools/make-standalone-toolchain.sh --platform=android-$APIVERSION --toolchain=$TOOLCHAIN --install-dir=$INSTALLDIR > $LOGFILE 2>&1 
    export TOOLCHAIN_PATH=`pwd`/android-toolchain-mips64/bin
    export TOOL=$TOOLNAME
    export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
    export CC=$NDK_TOOLCHAIN_BASENAME-gcc
    export CXX=$NDK_TOOLCHAIN_BASENAME-g++
    export LINK=${CXX}
    export LD=$NDK_TOOLCHAIN_BASENAME-ld
    export AR=$NDK_TOOLCHAIN_BASENAME-ar
    export RANLIB=$NDK_TOOLCHAIN_BASENAME-ranlib
    export STRIP=$NDK_TOOLCHAIN_BASENAME-strip
    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/$SUBPLATFORM/include
    export LIBS=-lssl

    echo "  Configure..."
    ./autogen.sh >>$LOGFILE 2>&1
    PATH=$TOOLCHAIN_PATH:$PATH CONFIG="./configure $PREFIX $FLAGS --host=$HOST"
    $CONFIG >>$LOGFILE 2>&1

    echo "  Make..."
    make >> $LOGFILE 2>&1 & spinner

    echo "  Make install..."
    make install >> $LOGFILE 2>&1 
}

build_Linux()
{
    rm -fr $LIBEVENTOUT
    rm -fr $LIBEVENTDIR
    tar -xzvf $TAR
    cd $LIBEVENTDIR

    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/include
    export LIBS=-lssl

    ./autogen.sh
    CONFIG="./configure $PREFIX $FLAGS"
    $CONFIG

    #Compile
    make 
    make install
}

build_Windows32()
{
    #Windows在mingw+msys的环境下编译，需要提前准备环境

    rm -fr $LIBEVENTOUT
    rm -fr $LIBEVENTDIR
    tar -xzvf $TAR
    cd $LIBEVENTDIR

    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/include

    ./autogen.sh
    CONFIG="./configure $PREFIX $FLAGS --disable-clock-gettime"
    $CONFIG

    #Compile
    make 
    make install
}

build_Windows64()
{
    #Windows在mingw+msys的环境下编译，需要提前准备环境

    rm -fr $LIBEVENTOUT
    rm -fr $LIBEVENTDIR
    tar -xzvf $TAR
    cd $LIBEVENTDIR

    export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/lib/pkgconfig:$PKG_CONFIG_PATH
    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/include

    ./autogen.sh
    CONFIG="./configure $PREFIX $FLAGS --disable-clock-gettime"
    $CONFIG

    #Compile
    make 
    make install
}

build_Linux-Embedded()
{
    rm -fr $LIBEVENTOUT
    rm -fr $LIBEVENTDIR
    tar -xzvf $TAR
    cd $LIBEVENTDIR

    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/include

    ./autogen.sh
    CONFIG="./configure $PREFIX $FLAGS"
    CC=arm-hisiv100nptl-linux-gcc $CONFIG

    #Compile
    make 
    make install
}

build_MacOS()
{
    rm -fr $LIBEVENTOUT
    rm -fr $LIBEVENTDIR
    tar -xzvf $TAR
    cd $LIBEVENTDIR

    export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/lib
    export CPPFLAGS=-I$DEPPATH/openssl-$PLATFORM/include

    ./autogen.sh
    CONFIG="./configure $PREFIX $FLAGS"
    $CONFIG

    #Compile
    make 
    make install
}

build_iOS()
{
    # To set "enable-ec_nistp_64_gcc_128" configuration for x64 archs set next variable to "true"
    ENABLE_EC_NISTP_64_GCC_128=""
    CONFIG_OPTIONS=""
    DEVELOPER=`xcode-select -print-path`
    SDKVERSION=`xcrun -sdk iphoneos --show-sdk-version`                   
    MIN_SDK_VERSION="7.0"

    ARCHS="i386 x86_64 armv7 armv7s arm64"

    CURRENTPATH=$DEPPATH

    for ARCH in ${ARCHS}
    do
        if [[ "${ARCH}" == "i386" || "${ARCH}" == "x86_64" ]]; then
            SUBPLATFORM="iPhoneSimulator"
            HOST=""
        else
            SUBPLATFORM="iPhoneOS"
            HOST="arm-apple-darwin"
        fi

        export $SUBPLATFORM
        export CROSS_TOP="${DEVELOPER}/Platforms/${SUBPLATFORM}.platform/Developer"
        export CROSS_SDK="${SUBPLATFORM}${SDKVERSION}.sdk"
        export BUILD_TOOLS="${DEVELOPER}"

        mkdir -p "${CURRENTPATH}/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk"
        LOG="${CURRENTPATH}/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk/build-libevent-${LIBEVENTVERSION}.log"

        echo "Building libevent-${VERSION} for ${SUBPLATFORM} ${SDKVERSION} ${ARCH}"
        echo "  Logfile: $LOG"


        if [[ "${SDKVERSION}" == 9.* || "${SDKVERSION}" == [0-9][0-9].* ]]; then
            export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH} -fembed-bitcode  -miphoneos-version-min=${MIN_SDK_VERSION}"
        else
            export CC="${BUILD_TOOLS}/usr/bin/gcc -arch ${ARCH} -miphoneos-version-min=${MIN_SDK_VERSION}"
        fi

        echo "  Patch source code..."

        src_work_dir="${CURRENTPATH}/src/${SUBPLATFORM}-${ARCH}"
        mkdir -p "$src_work_dir"
        tar zxf "${CURRENTPATH}/${TAR}" -C "$src_work_dir"
        cd "${src_work_dir}/$TARBASENAME"

        (./autogen.sh > "${LOG}" 2>&1) & spinner

    	export PKG_CONFIG_PATH=$DEPPATH/openssl-$PLATFORM/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/pkgconfig:$PKG_CONFIG_PATH
    	export LDFLAGS=-L$DEPPATH/openssl-$PLATFORM/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk/lib
    	export CPPFLAGS="-I$DEPPATH/openssl-$PLATFORM/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk/include -isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK}"
        export CFLAGS="$CFLAGS -isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK}"
        PREFIX=--prefix=$LIBEVENTOUT/${SUBPLATFORM}${SDKVERSION}-${ARCH}.sdk

        echo "  configure..."
        set +e
        if [ "$1" == "verbose" ]; then
            ./configure $PREFIX $FLAGS --host=$HOST
        else
            (./configure $PREFIX $FLAGS --host=$HOST > "${LOG}" 2>&1 ) & spinner
        fi

        if [ $? != 0 ]; then
            echo "Problem while configure - Please check ${LOG}"
            exit 1
        fi

        echo "  Patch Makefile..."
        # add -isysroot to CC=
        sed -ie "s!^CFLAG=!CFLAG=-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=${MIN_SDK_VERSION} !" "Makefile"

        echo "  Make..."

        if [ "$1" == "verbose" ]; then
            if [[ ! -z $CONFIG_OPTIONS ]]; then
                make 
            fi
            make
        else
            if [[ ! -z $CONFIG_OPTIONS ]]; then
                make >> "${LOG}" 2>&1
            fi
            (make >> "${LOG}" 2>&1) & spinner
        fi
        echo "\n"

        if [ $? != 0 ]; then
            echo "Problem while make - Please check ${LOG}"
            exit 1
        fi

        set -e
        if [ "$1" == "verbose" ]; then
            make install
            make clean
        else
            make install >> "${LOG}" 2>&1
            make clean >> "${LOG}" 2>&1
        fi

        rm -rf "$src_work_dir"
    done

    echo "Build library for iOS..."
    mkdir -p ${LIBEVENTOUT}/lib
    lipo -create \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-i386.sdk/lib/libevent.a \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-x86_64.sdk/lib/libevent.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7.sdk/lib/libevent.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7s.sdk/lib/libevent.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-arm64.sdk/lib/libevent.a \
      -output ${LIBEVENTOUT}/lib/libevent.a
    lipo -create \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-i386.sdk/lib/libevent_openssl.a \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-x86_64.sdk/lib/libevent_openssl.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7.sdk/lib/libevent_openssl.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7s.sdk/lib/libevent_openssl.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-arm64.sdk/lib/libevent_openssl.a \
      -output ${LIBEVENTOUT}/lib/libevent_openssl.a
	  
	lipo -create \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-i386.sdk/lib/libevent_pthreads.a \
      ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-x86_64.sdk/lib/libevent_pthreads.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7.sdk/lib/libevent_pthreads.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-armv7s.sdk/lib/libevent_pthreads.a \
      ${LIBEVENTOUT}/iPhoneOS${SDKVERSION}-arm64.sdk/lib/libevent_pthreads.a \
      -output ${LIBEVENTOUT}/lib/libevent_pthreads.a

    echo "Build include files"
    mkdir -p ${LIBEVENTOUT}/include
    cp -R ${LIBEVENTOUT}/iPhoneSimulator${SDKVERSION}-x86_64.sdk/include/* ${LIBEVENTOUT}/include/
}


if [ "$PLATFORM" == "Linux" ]
then
    build_Linux
elif [ "$PLATFORM" == "LinuxEmbedded" ]
then
    build_Linux-Embedded
elif [ "$PLATFORM" == "Windows32" ]
then
    build_Windows32
elif [ "$PLATFORM" == "Windows64" ]
then
    build_Windows64
elif [ "$PLATFORM" == "MacOS" ]
then
    build_MacOS
elif [ "$PLATFORM" == "iOS" ]
then
    build_iOS
elif [ "$PLATFORM" == "Android" ]
then
    build_Android
else
    echo "unrecognized platform : $PLATFORM"
    echo "Platform:Linux/LinuxEmbedded/Windows32/Windows64/MacOS/iOS/Android"
    exit 1
fi

